#pragma once

#include <string.h>

#include <algorithm>
#include <list>
#include <map>
#include <mutex>
#include <string>
#include <vector>

#include "bondlib.h"

typedef struct _tag_BOND_CRM_CLAUSE {  // CRM
  char BondKey[33];
  char ReferenceCode[33];       // 参考实体, 对应IssuerID
  char SettlementModes[128];    // 结算方式
  char SettlementModesEn[128];  // 结算方式英文
  int RegistrationDate;         // 凭证登记日
  double FirstIssueAmount;      // 预配售额
  std::string CreaterCode;      // 创设机构, 对应IssuerID
  std::string ReferBondKey;     // 参考债券，对应BondKey.ListedMarket
  std::string CreditEvent;      // 信用事件
  std::string CreditEventEn;    // 信用事件英文
  _tag_BOND_CRM_CLAUSE() { clear(); };
  void clear() {
    memset(BondKey, 0, sizeof(BondKey));
    memset(ReferenceCode, 0, sizeof(ReferenceCode));
    memset(SettlementModes, 0, sizeof(SettlementModes));
    memset(SettlementModesEn, 0, sizeof(SettlementModesEn));
    RegistrationDate = 0;
    FirstIssueAmount = 0;
    CreaterCode.clear();
    ReferBondKey.clear();
    CreditEvent.clear();
    CreditEventEn.clear();
  };
} BOND_CRM_CLAUSE;

class S_BONDLIB_EXPORT QBCRMBond {
 public:
  static QBCRMBond& instance();

 public:
  int GetSize();
  const BOND_CRM_CLAUSE* GetBondByKey(const std::string& bondkey);
  const BOND_CRM_CLAUSE* GetBondByReferKey(const std::string& bondkey);
  const BOND_CRM_CLAUSE* GetAt(int nIndex);

  bool Clear();
  bool Add(const BOND_CRM_CLAUSE& info);

  std::mutex& GetMutex() { return m_mutex; }

 public:
  void DumpStatus(int mode = 0) const;

 private:
  QBCRMBond();
  virtual ~QBCRMBond();

 protected:
  std::vector<BOND_CRM_CLAUSE> m_data;
  std::map<std::string, int> m_map;
  std::map<std::string, int> m_refmap;
  std::mutex m_mutex;
};
